package com.useeinfo.oa.open.api.inspection;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.AuditFlowBiz;
import com.useeinfo.oa.modules.base.entity.AuditFlow;
import com.useeinfo.oa.modules.inspection.biz.InspectionOrderBiz;
import com.useeinfo.oa.modules.inspection.biz.PlannerInspectionBiz;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.maintaining.biz.PeriodInstallBiz;
import com.useeinfo.oa.modules.maintaining.biz.PlannerOrderBiz;
import com.useeinfo.oa.modules.maintaining.entity.PeriodInstall;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 维护保养模块计划工单app接口
 */
@RestController
@RequestMapping("/api/plannerInspection")
public class PlannerInspectionAPI {

    @Autowired
    private PlannerInspectionBiz plannerInspectionBiz;
    @Autowired
    private InspectionOrderBiz inspectionOrderBiz;
    @Autowired
    private PeriodInstallBiz periodInstallBiz;
    @Autowired
    private AuditFlowBiz auditFlowBiz;


    /**
     * 保存周期计划
     */
    @RequestMapping("/savePlannerInspection")
    @ResponseBody
    public ResultDto savePlannerInspection(String name,String degree,String startTime,String endTime,String required,String remindTime,String periodId,
                                      String  assignUser,String assignPosition,String completeAuditId,String delayAuditId,String cancelAuditId,
                                      String inspectionData,String type, HttpServletRequest request) {
        if("1".equals(type)){
            return plannerInspectionBiz.savePlanner(name,degree,startTime,endTime,required,remindTime,periodId,assignUser,assignPosition,completeAuditId,
                    delayAuditId,cancelAuditId,inspectionData,request);
        }else{
            return inspectionOrderBiz.saveOrder(name,degree,endTime,required,remindTime,assignUser,assignPosition,completeAuditId,
                    delayAuditId,cancelAuditId,inspectionData,request);
        }

    }

    /**
     * 时间周期列表
     * @param request
     * @return
     */
    @RequestMapping("/periodList")
    @ResponseBody
    public ResultDto periodList( HttpServletRequest request) {
        JSONArray jsonArray=new JSONArray();
        QueryParam queryParam=new QueryParam("-1","-1");
        queryParam.getSqlMap().put("enterpriseId",SessionKeyUtils.getSessionEnterprise(request).getId()+"");
        List<PeriodInstall>periodInstalls=periodInstallBiz.findList(queryParam);
        periodInstalls.forEach(x->{
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("name",x.getName());
            jsonObject.put("id",x.getId());
            jsonArray.add(jsonObject);

        });
       return ResultUtil.success(jsonArray);
    }

    /**
     * 审核流接口
     * @param request
     * @param type
     * @return
     */
    @RequestMapping("/auditFlowList")
    @ResponseBody
    public ResultDto auditFlowList( HttpServletRequest request,String type) {
        JSONArray jsonArray=new JSONArray();
        QueryParam queryParam=new QueryParam("-1","-1");
        queryParam.getSqlMap().put("enterpriseId",SessionKeyUtils.getSessionEnterprise(request).getId()+"");
        queryParam.getSqlMap().put("type",type);
        String moduleType="INSPECTION"; //1,维修管理，2维护保养，3巡检
        queryParam.getSqlMap().put("remark",moduleType);

        List<AuditFlow>auditFlows=auditFlowBiz.findList(queryParam);
        auditFlows.forEach(x->{
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("name",x.getFlowName());
            jsonObject.put("id",x.getId());
            jsonArray.add(jsonObject);
        });
        return ResultUtil.success(jsonArray);
    }









}
